<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // class Animal{
        //     constructor(name){
        //         this.name = name;
        //     }

        //     shout(){
        //         console.log(this.name);
        //     }
        // }

        // class Cat extends Animal{

        // }

        // function Animal(name){
        //     this.name = name;
        // }
        // Animal.prototype.shout = function(){
        //     console.log(this.name);
        // }

        // function Cat(){

        // }

        // var p1 = new Animal('咪咪');
        // p1.shout();

        function ClassA(sColor) {
            this.color = sColor;
        }

        // prototype是构造函数下面的一个属性
        ClassA.prototype.sayColor = function () {
            console.log(this.color);
        };
        ClassA.prototype.sayHi = function () {
            //...
        };

        function ClassB(sColor) {
            // 用ClassA.call继承ClassA中的属性
            ClassA.call(this, sColor);
        }

        // inherit用来继承obj的prototype中所有的方法，而不会继承obj中的属性
        // 寄生组合
        function inherit(obj) {
            var newObj = new Object();
            for (var prop in obj) { //p: property
                newObj[prop] = obj[prop];
            }
            return newObj;
        }

        // ClassB.prototype = new ClassA();
        // ClassB.prototype = inherit(ClassA.prototype);
        ClassB.prototype = Object.create(ClassA.prototype);
        ClassB.prototype.constructor = ClassB;

        // console.log(ClassB.prototype.color);


        ClassB.prototype.xx = function () {
            console.log(this.color);
        };


        var c1 = new ClassB('red');
        // c1.xx();
        console.log(c1.color);
        // c1.sayColor();

        // var c2 = new ClassA('blue');
        // c2.xx();


    </script>
</body>

</html>